import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login.vue'
import Home from '../components/Home.vue'
// import { from } from 'webpack-sources/lib/CompatSource';
Vue.use(VueRouter)

const routes = [
  { path: '/', component: Login },
  { path: '/login', component: Login },
  { path: '/home', component: Home }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  // next() 放行   next('/login) 强制跳转
  // console.log(to.path)
  if (to.path == '/login') return next()
  // 获取token
  const tokenStr = window.sessionStorage.getItem('token')
  // 若没有token  则强制跳转到登录页
  if (!tokenStr) return next('/login')
  // 否则 放行
  next()
})

export default router
